﻿package j.sus2maj7.util
{
	
	import flash.utils.ByteArray;
	
	public class ByteUtil
	{
		public static function copy(ba:ByteArray):ByteArray
		{
			var copy:ByteArray = new ByteArray();
			
			var len:uint = ba.length;
			for (var i:uint = 0; i < len; i++)
			{
				copy[i] = ba[i];
			}
			
			return copy;
		}
		
		public static function print(bytes:ByteArray):String
		{
			var strHex:String = "";
			var strAscii:String = "";
			
			var retVal:String = "";
			var buffer:String = "";
			
			var len:int = bytes.length;
			for (var i:Number = 0; i < len; i++)
			{
				if (i % 16 == 0)
				{
					retVal += strHex + " " + strAscii + "\n";
					
					strHex = "";
					strAscii = "";
				}

				if (i % 8 == 0 && i % 16 != 0)
				{
					strHex += " ";
					strAscii += " ";
				}
				
				var byte:uint = bytes[i];
				
				if (byte < 16)
				{
					strHex += "0" + byte.toString(16) + " ";
				}
				else
				{
					strHex += byte.toString(16) + " ";
				}
				
				if (byte < 127 && byte >= 32)
				{
					strAscii += "" + String.fromCharCode(byte);
				}
				else
				{
					strAscii += ".";
				}
			}
			
			while (strHex.length < 50)
			{
				strHex += ' ';
			}
			
			
			retVal +=  strHex + buffer + strAscii + "\n";
			
			return retVal;
		}
	}

}